﻿#region Copyright

// The contents of this file are subject to the Mozilla Public License
//  Version 1.1 (the "License"); you may not use this file except in compliance
//  with the License. You may obtain a copy of the License at
//  
//  http://www.mozilla.org/MPL/
//  
//  Software distributed under the License is distributed on an "AS IS"
//  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//  License for the specific language governing rights and limitations under 
//  the License.
//  
//  The Initial Developer of the Original Code is Robert Smyth.
//  Portions created by Robert Smyth are Copyright (C) 2008.
//  
//  All Rights Reserved.

#endregion

using NUnitGrid.Framework;
using NUnitGrid.Framework.Logging;
using NUnitGrid.Framework.Pipes;
using NUnitGrid.Protocol;
using NUnitGrid.Protocol.Commands;


namespace NUnitGrid.Server.Plugins.Nodes.Node.States
{
    public class StateFactory<T> : IServerStateFactory
        where T : IGridNode
    {
        private readonly IGridMetrics gridMetrics;
        private readonly IGridNodeMetrics gridNodeMetrics;
        private readonly IReadInfoListener infoListener;
        private readonly ILogger logger;
        private readonly T node;
        private readonly INodeAvailableListener<T> nodeAvailableListener;
        private readonly IPipe pipe;
        private readonly IGridProtocol protocol;
        private readonly IReadInfoListener readInfoListener;
        private readonly ISchedule schedule;
        private readonly IStateEngine<IGridNodeState> stateEngine;

        public StateFactory(IGridProtocol protocol, IStateEngine<IGridNodeState> stateEngine, ISchedule schedule,
                            IPipe pipe, ILogger logger, INodeAvailableListener<T> nodeAvailableListener,
                            IReadInfoListener readInfoListener, T node, IReadInfoListener infoListener,
                            IGridNodeMetrics gridNodeMetrics, IGridMetrics gridMetrics)
        {
            this.protocol = protocol;
            this.stateEngine = stateEngine;
            this.pipe = pipe;
            this.schedule = schedule;
            this.logger = logger;
            this.nodeAvailableListener = nodeAvailableListener;
            this.readInfoListener = readInfoListener;
            this.node = node;
            this.infoListener = infoListener;
            this.gridNodeMetrics = gridNodeMetrics;
            this.gridMetrics = gridMetrics;
        }

        public IGridNodeState CreateStoppedState()
        {
            return new StoppedState(stateEngine, this);
        }

        public IGridNodeState CreateStartingState()
        {
            return new StartingState<T>(stateEngine, this, protocol, readInfoListener, schedule, nodeAvailableListener,
                                        node, gridMetrics);
        }

        public IGridNodeState CreateAvailableState()
        {
            return new AvailableState<T>(stateEngine, this, schedule, protocol, nodeAvailableListener, node,
                                         infoListener);
        }

        public IGridNodeState CreateFailedState()
        {
            return new FailedState();
        }

        public IGridNodeState CreateLinkNotConnectedState()
        {
            return new LinkNotConnectedState(stateEngine, this, pipe, logger);
        }

        public IGridNodeState CreateRunningTaskState(IRunOnGridTask task, IRunOnGridTaskListener taskListener,
                                                     IGridNodeListener<IGridNodeMetrics> nodeListener)
        {
            return new RunningTaskState<T>(stateEngine, this, schedule, protocol, task, taskListener, nodeListener,
                                           gridNodeMetrics, node, nodeAvailableListener);
        }
    }
}